package com.cdel.akka.AkkainJAVA.demo4router;

import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.routing.ActorRefRoutee;
import akka.routing.CustomRouterConfig;
import akka.routing.RoundRobinRoutingLogic;
import akka.routing.Routee;
import akka.routing.Router;
import java.util.ArrayList;
import java.util.List;

//这种方式是通过创建一个RouteActor来使用路由.RouteActor和一般的Actor没有什么不同,
//区别在于它没有什么业务逻辑,在创建它的时候,它会创建N个具备业务逻辑的子Actor.当它接收到消息后,会把消息转发给它的某个子Actor.
public class BurstyMessageRouter extends CustomRouterConfig {

    private int noOfInstances;

    public BurstyMessageRouter(int inNoOfInstances) {
        noOfInstances = inNoOfInstances;
    }

    @Override
    public Router createRouter(ActorSystem system) {
        final List<Routee> routees = new ArrayList<Routee>(noOfInstances);
        for (int i = 0; i < noOfInstances; i++) {
            routees.add(new ActorRefRoutee(system.actorOf(Props.create(WriterActor.class), "actor-" + String.valueOf(i))));
        }
        /**
         * 使用轮询调度算法的router
         */
        return new Router(new RoundRobinRoutingLogic(), routees);
    }

}